import echarts from 'echarts'
export default {
  data() {
    return {
      lineChart: null
    }
  },
  methods: {
    renderLineChart(data) {
      let xlabel = [],
        dataObj = {
          data0: [],
          data1: [],
          data2: []
        }
      const colorList = [
        ["#9E87FF", '#9effff', '#9E87FF', 'rgba(158,135,255, 0.3)'],
        ['#73DDFF', '#73DD39', '#73DDFF', 'rgba(115,221,255, 0.3)'],
        ['#fe9a8b', '#fe9a', '#fe9a8b', 'rgba(254,154,139, 0.3)'],
      ]

      data.forEach((item, i) => {
        xlabel.push(item.date.substring(5))
        dataObj.data0.push(item.received)
        dataObj.data1.push(item.receiving)
        dataObj.data2.push(item.total)
      });
      let series = ['已收', '未收', '应收'].map((_name, i) => {
        return {
          name: _name,
          type: 'line',
          data: dataObj[`data${i}`],
          symbolSize: 1,
          symbol: 'circle',
          smooth: true,
          showSymbol: false,
          lineStyle: {
            width: 3,
            color: new echarts.graphic.LinearGradient(0, 1, 0, 0, [{
                offset: 0,
                color: colorList[i][1],
              },
              {
                offset: 1,
                color: colorList[i][2],
              }
            ]),
            shadowColor: colorList[i][3],
            shadowBlur: 5,
            shadowOffsetY: 10
          },
          itemStyle: {
            normal: {
              color: colorList[i][0],
              borderColor: colorList[i][0]
            }
          }
        }
      })
      let option = {
        legend: {
          icon: 'circle',
          top: '5%',
          right: 'center',
          itemWidth: 6,
          itemGap: 20,
          textStyle: {
            color: '#556677'
          }
        },
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            label: {
              show: true,
              backgroundColor: '#fff',
              color: '#556677',
              borderColor: 'rgba(0,0,0,0)',
              shadowColor: 'rgba(0,0,0,0)',
            },
            lineStyle: {
              width: 0
            }
          },
          backgroundColor: '#fff',
          textStyle: {
            color: '#5c6c7c'
          },
          padding: [10, 10],
          extraCssText: 'box-shadow: 1px 0 2px 0 rgba(163,163,163,0.5)'
        },
        grid: {
          top: '15%'
        },
        xAxis: {
          type: 'category',
          data: xlabel,
          axisLine: {
            lineStyle: {
              color: '#DCE2E8'
            }
          },
          axisTick: {
            show: false
          },
          axisLabel: {
            rotate: 45,
            interval: 0,
            textStyle: {
              color: '#556677'
            },
            // 默认x轴字体大小
            fontSize: 12,
            // margin:文字到x轴的距离
            margin: 15
          },
          boundaryGap: false
        },
        yAxis: {
          type: 'value',
          axisTick: {
            show: false
          },
          axisLine: {
            show: true,
            lineStyle: {
              color: '#DCE2E8'
            }
          },
          axisLabel: {
            textStyle: {
              color: '#556677'
            }
          },
          splitLine: {
            show: true
          }
        },
        series
      }

      this.lineChart.setOption(option)
    }
  },
}
